<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
      <div class="logingR">
        <div class="title-container">
          <img src="../../assets/title.ef005a7a.png" alt="">
        </div>
        <div class="ipote">
          <el-form-item prop="username" class="admin">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
              style="background-color:brown;"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual style="width:100%; margin-top:40px;">
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
        </div>
        <div class="buttenn">
          <el-button :loading="loading" type="primary" style="width:100%;height:50px; margin-bottom:30px;" @click.native.prevent="handleLogin">立即登录</el-button>
        </div>

      </div>
      <div class="logingL">
        <!-- <img src="../../assets/contentBg.1321d126.png" alt=""> -->
      </div>
    </el-form>
  </div>

</template>

<script>
import { nanoid } from 'nanoid'
import { test } from '@/api'
import { validUsername } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        mobile: 'admin',
        password: '111111'
      },
      loginRules: {
        mobile: [{ required: true, message: '请输入用户名', trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, message: '请输入用户名', trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    test()
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录执行的方法
    handleLogin() {
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) return
        await this.$store.dispatch('user/login', nanoid())
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="scss">

$bg:#fff;
$light_gray:#fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .el-input {
    // display: inline-block;
    height: 0px;
    width: 89%;
    // background-color: #fff;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      height: 50px;
      // caret-color: #f8f5f5;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px  inset !important;
      //   // -webkit-text-fill-color: $cursor !important;
      // }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: block;

  .login-form{
    position: relative;
    width: 958px;
    max-width: 100%;
    // padding: 160px 35px 0;
    // margin: 0 auto;
    // overflow: hidden;
    height: 516px;
    // background-color: #df8e76;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 0 0 20px rgb(93 93 93 / 33%);

  }
  .logingR {
    float: left;
    width: 398px;
    height: 516px;
    // background: #fff;
    border-radius: 34px 0 0 34px;
    margin: 0 auto;
    padding: 50px,0,50px, 0;
  }
  .logingL {
    float: right;
    background-image: url(../../assets/contentBg.1321d126.png);
    background-position: 50%;
    background-repeat: no-repeat;
    background-size: cover;
    width: 560px;
    height: 516px;
  }
  .ipote {
    padding: 50px;
    // margin-top: 30px;
  }
  .buttenn {
    padding-left: 50px;
    padding-right: 50px;
  }
  .tips {
    font-size: 14px;
    // color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    // color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    margin-left: 124px;
    margin-top: 72px;
    img {
      width: 150px;
    height: 64px;
    }
    .title {
      font-size: 26px;
      // color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    // color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
